package com.javabasic.concurrency;

/**
 * @Author xiongmin
 * @ClassName Wait_notiffy
 * @Description TODO
 * @Date 2019/8/14 16:31
 * @Version 1.0
 **/
public class Wait_notiffy {
    private final static Object LOCK = new Object();

    public static void main(String[] args) {
        System.out.println(tableSizeFor(10));
    }

    /**
     * 测试wait和notify  的使用
     */
    static public void testThread() {
        new Thread(new Runnable(){
            @Override
            public void run() {
                synchronized (LOCK) {
                    System.out.println("hello  我是 wait pre");
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("hello  我是 wait post");
                }
            }
        }).start();
        new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                    synchronized (LOCK) {
                        LOCK.notifyAll();
                        System.out.println("hello  我是 notify");
                        Thread.sleep(10000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 返回一个中是比参数大且最小的2的幂
     * @param var0
     * @return
     */
    static final int tableSizeFor(int var0) {
        int var1 = var0 - 1;
        var1 |= var1 >>> 1;
        var1 |= var1 >>> 2;
        var1 |= var1 >>> 4;
        var1 |= var1 >>> 8;
        var1 |= var1 >>> 16;
        return var1 < 0 ? 1 : (var1 >= 1073741824 ? 1073741824 : var1 + 1);
    }
}
